package com.yanqu.road.server.gameplayer.module.activity.busyfarming;

import com.yanqu.road.entity.activity.busyfarming.*;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class BusyFarmingPb {


    /**
     * 玩家数据
     */
    public static BusyFarmingProto.BusyFarmingUserDataTemp.Builder getUserDataBuilder(BusyFarmingUserData userData, boolean containLand) {
        BusyFarmingProto.BusyFarmingUserDataTemp.Builder dataBuilder = BusyFarmingProto.BusyFarmingUserDataTemp.newBuilder();
        dataBuilder.setBeStealTimes(userData.getBeStealTimes());
        dataBuilder.setBeWateringTimes(userData.getBeWateringTimes());
        dataBuilder.setFruitScore(userData.getFruitScore());
        dataBuilder.setHomeGrade(userData.getHomeGrade());
        dataBuilder.setWateringTimes(userData.getWateringTimes());
        dataBuilder.setStealTimes(userData.getStealTimes());
        if(containLand){
            for(BusyFarmingLand land : userData.getLandMap().values()){
                dataBuilder.addLand(getLandBuilder(land));
            }
        }
        dataBuilder.setUserId(userData.getUserId());
        return dataBuilder;
    }

    /**
     * 跨服玩家数据
     */
    public static BusyFarmingProto.CrossUserDataTemp.Builder getCrossUserDataBuilder(BusyFarmingUserData userData) {
        BusyFarmingProto.CrossUserDataTemp.Builder dataBuilder = BusyFarmingProto.CrossUserDataTemp.newBuilder();
        dataBuilder.setHomeGrade(userData.getHomeGrade());
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userData.getUserId(), GameServer.getInstance().getServerId());
        dataBuilder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        dataBuilder.setUserId(userData.getUserId());
        for(BusyFarmingLand land : userData.getLandMap().values()){
            dataBuilder.addLand(getLandBuilder(land));
        }
        return dataBuilder;
    }

    /**
     * 土地数据
     */
    public static BusyFarmingProto.BusyFarmingLandTemp.Builder getLandBuilder(BusyFarmingLand land){
        BusyFarmingProto.BusyFarmingLandTemp.Builder builder = BusyFarmingProto.BusyFarmingLandTemp.newBuilder();
        builder.setId(land.getId());
        builder.setGoodsId(land.getGoodsId());
        builder.setHarvestTime(land.getHarvestTime());
        builder.setTotalTime(land.getTotalTime());
        builder.addAllStates(land.getStates());
        builder.setFlorescence(land.isFlorescence());
        builder.setBeWateringTimes(land.getWateringTimes());
        return builder;
    }

    /**
     * 系统配置
     */
    public static BusyFarmingProto.BusyFarmingConfigDataTemp.Builder getBusyFarmingConfigDataTempBuilder(String language){
        BusyFarmingProto.BusyFarmingConfigDataTemp.Builder builder = BusyFarmingProto.BusyFarmingConfigDataTemp.newBuilder();
        builder.setOneKeyManureGrade(BusyFarmingMgr.BUSY_FARMING_ONE_KEY_MANURE_GRADE);
        builder.setWateringTimes(BusyFarmingMgr.getBusyFarmingWateringParam().get(0));
        builder.setStealOpenHour(BusyFarmingMgr.getBusyFarmingOptOpenTimeParam().get(0));
        List<Integer> timeList = new ArrayList<>();
        timeList.add(BusyFarmingMgr.getBusyFarmingOptOpenTimeParam().get(1));
        timeList.add(BusyFarmingMgr.getBusyFarmingOptOpenTimeParam().get(2));
        builder.addAllFarmingOpenTime(timeList);
        builder.setStealTimes(BusyFarmingMgr.getBusyFarmingStolenParam().get(1));
        for(BusyFarmingGradeInfo gradeInfo : BusyFarmingMgr.getGradeInfoList()){
            builder.addGradeName(ServerLanguageMgr.getContent(gradeInfo.getName(), language));
        }
        builder.setLandOpenConsume(StringUtils.listToString(BusyFarmingMgr.BUSY_FARMING_LAND_OPEN_CONSUME, "|"));
        builder.setSeedStockTipsVal(BusyFarmingMgr.BUSY_FARMING_SEED_STOCK_TIPS_VAL);
        return builder;
    }

    /**
     * 酬谢
     */
    public static BusyFarmingProto.BusyFarmingUserRewardTemp.Builder getUserRewardBuilder(BusyFarmingUserReward reward) {
        BusyFarmingProto.BusyFarmingUserRewardTemp.Builder builder = BusyFarmingProto.BusyFarmingUserRewardTemp.newBuilder();
        builder.setDbId(reward.getDbId());
        builder.setFriendId(reward.getFriendId());
        builder.setGetReward(reward.isGetReward());
        builder.setHelpType(reward.getHelpType());
        builder.setReward(reward.getReward());
        builder.addAllParams(reward.getParams());
        builder.setTime(reward.getAddTime());

        UserBaseInfo userBaseInfo = null;
        UserInfo me = UserMgr.getUserInfo(reward.getUserId());
        //是否商会成员
        CrossUnionMember member = CrossUnionMgr.getUnionMember(me.getUnionUid(), reward.getFriendId());
        if(member != null && member.getUserBaseInfo() != null){
            if(member.getServerId() == GameServer.getInstance().getServerId()) {
                userBaseInfo = UserMgr.getUserBaseInfo(reward.getFriendId(), GameServer.getInstance().getServerId());
                if(userBaseInfo != null) {
                    builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                }else {
                    builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(new UserBaseInfo()));
                }
            }else {
                builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(member.getUserBaseInfo()));
            }
        }else {
            userBaseInfo = UserMgr.getUserBaseInfo(reward.getFriendId(), GameServer.getInstance().getServerId());
            if(userBaseInfo != null) {
                builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }else {
                builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(new UserBaseInfo()));
            }
        }
        return builder;
    }

    /**
     * 酬谢
     */
    public static List<BusyFarmingProto.BusyFarmingUserRewardTemp> getBusyFarmingUserRewardTemps(List<BusyFarmingUserReward> rewardList) {
        List<BusyFarmingProto.BusyFarmingUserRewardTemp> builderList = new ArrayList<>();
        int size = rewardList.size();
        int num = 0;
        for(int i = size - 1; i >= 0; i--){
            BusyFarmingUserReward reward = rewardList.get(i);
            if(reward.isDel()){
                continue;
            }
            BusyFarmingProto.BusyFarmingUserRewardTemp.Builder builder = getUserRewardBuilder(reward);
            builderList.add(builder.build());
            num++;
            if(num >= BusyFarmingMgr.RETAIN_REWARD_DATA_NUM){
                break;
            }
        }
        return builderList;
    }

    /**
     * 来助
     */
    public static BusyFarmingProto.BusyFarmingHelpRecordTemp.Builder getHelpRecordBuilder(BusyFarmingHelpRecord record) {
        BusyFarmingProto.BusyFarmingHelpRecordTemp.Builder builder = BusyFarmingProto.BusyFarmingHelpRecordTemp.newBuilder();
        builder.setFriendId(record.getFriendId());
        builder.setHelpType(record.getHelpType());
        builder.addAllParams(record.getParams());
        builder.setHelpTime(record.getHelpTime());
        if(record.getUserBaseInfo() != null) {
            builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(record.getUserBaseInfo()));
        }else {
            UserInfo me = UserMgr.getUserInfo(record.getUserId());
            //是否商会成员
            CrossUnionMember member = CrossUnionMgr.getUnionMember(me.getUnionUid(), record.getFriendId());
            if(member != null && member.getUserBaseInfo() != null && member.getServerId() != GameServer.getInstance().getServerId()) {
                builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(member.getUserBaseInfo()));
            }else {
                UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(record.getFriendId(), GameServer.getInstance().getServerId());
                if(userBaseInfo != null){
                    builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                }else {
                    builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(new UserBaseInfo()));
                }
            }
        }
        return builder;
    }

    /**
     * 失窃记录
     */
    public static BusyFarmingProto.BusyFarmingStolenListTemp.Builder getUserStolenBuilder(BusyFarmingStolenRecord record) {
        BusyFarmingProto.BusyFarmingStolenListTemp.Builder builder = BusyFarmingProto.BusyFarmingStolenListTemp.newBuilder();
        builder.setEnemyId(record.getEnemyId());
        builder.setEnemyServerId(record.getEnemyServerId());
        builder.setStolenTime(record.getStolenTime());
        builder.addAllParams(record.getParams());
        if(record.getUserBaseInfo() == null && record.getEnemyServerId() == GameServer.getInstance().getServerId()){
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(record.getEnemyId(), record.getEnemyServerId());
            builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }else {
            builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(record.getUserBaseInfo()));
        }
        return builder;
    }
    /**
     * 构建亲友果园简况
     */
    public static BusyFarmingProto.BusyFarmingFriendHomeTemp.Builder getHomeTempBuilder(BusyFarmingUserData userData, boolean canWatering, String language) {
        BusyFarmingProto.BusyFarmingFriendHomeTemp.Builder builder = BusyFarmingProto.BusyFarmingFriendHomeTemp.newBuilder();
        builder.setUserId(userData.getUserId());
        builder.setHomeGrade(userData.getHomeGrade());
        builder.setHomeGradeName(BusyFarmingMgr.getHomeGradeName(userData.getHomeGrade(), language));
        builder.setWatering(canWatering);
        UserBaseInfo baseInfo = UserMgr.getUserBaseInfo(userData.getUserId(), GameServer.getInstance().getServerId());
        builder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(baseInfo));
        return builder;
    }

    /**
     * 构建待偷取数据
     */
    public static BusyFarmingProto.BusyFarmingUserDataTemp.Builder parseBusyFarmingUserDataBuilder(BusyFarmingProto.CrossBusyFarmingStealDataTemp stealDataTemp) {
        BusyFarmingProto.BusyFarmingUserDataTemp.Builder builder = BusyFarmingProto.BusyFarmingUserDataTemp.newBuilder();
        if(stealDataTemp.getUserBaseInfo() != null) {
            builder.setUserBaseInfo(stealDataTemp.getUserBaseInfo());
        }
        builder.setUserId(stealDataTemp.getUserId());
        builder.setHomeGrade(stealDataTemp.getGrade());
        builder.addAllLand(stealDataTemp.getLandList());
        //默认值
        builder.setStealTimes(0);
        builder.setBeStealTimes(0);
        builder.setWateringTimes(0);
        builder.setBeWateringTimes(0);
        builder.setFruitScore(0);
        return builder;
    }
}
